package org.lmd.platform.controller.system;

import org.lmd.platform.constant.Constant;
import org.lmd.platform.controller.BaseController;
import org.lmd.platform.entity.sys.OrgEntity;
import org.lmd.platform.entity.sys.ReturnObjct;
import org.lmd.platform.sys.OrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/sys/dept")
public class OrgController extends BaseController {

    @Autowired
    private OrgService orgService;

    /**
     * 上级部门Id(管理员则为0)
     */
    @RequestMapping("/info")
    public ReturnObjct info() {
        long deptId = 0;
        if (getUserId() != Constant.SUPER_ADMIN) {
            List<OrgEntity> deptList = orgService.queryList(new HashMap<String, Object>());
            Long parentId = null;
            if (deptList != null && !deptList.isEmpty()) {
                for (OrgEntity sysDeptEntity : deptList) {
                    if (parentId == null) {
                        parentId = sysDeptEntity.getParentId();
                        continue;
                    }

                    if (parentId > sysDeptEntity.getParentId().longValue()) {
                        parentId = sysDeptEntity.getParentId();
                    }
                }
            } else {
                parentId = 0l;
            }

            deptId = parentId;
        }

        return ReturnObjct.ok().put("deptId", deptId);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:dept:list")
    public List<OrgEntity> list() {
        List<OrgEntity> deptList = orgService.queryList(new HashMap<String, Object>());

        return deptList;
    }

    /**
     * 选择部门(添加、修改菜单)
     */
    @RequestMapping("/select")
//    @RequiresPermissions("sys:dept:select")
    public ReturnObjct select() {
        List<OrgEntity> deptList = orgService.queryList(new HashMap<String, Object>());

        //添加一级部门
        if (getUserId() == Constant.SUPER_ADMIN) {
            OrgEntity root = new OrgEntity();
            root.setDeptId(0L);
            root.setName("一级部门");
            root.setParentId(-1L);
            root.setOpen(true);
            deptList.add(root);
        }

        return ReturnObjct.ok().put("deptList", deptList);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{deptId}")
//    @RequiresPermissions("sys:dept:info")
    public ReturnObjct info(@PathVariable("deptId") Long deptId) {
        OrgEntity dept = orgService.selectById(deptId);

        return ReturnObjct.ok().put("dept", dept);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:dept:save")
    public ReturnObjct save(@RequestBody OrgEntity dept) {
        orgService.insert(dept);

        return ReturnObjct.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:dept:update")
    public ReturnObjct update(@RequestBody OrgEntity dept) {
        orgService.updateById(dept);

        return ReturnObjct.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:dept:delete")
    public ReturnObjct delete(long deptId) {
        //判断是否有子部门
        List<Long> deptList = orgService.queryDetpIdList(deptId);
        if (deptList.size() > 0) {
            return ReturnObjct.error("请先删除子部门");
        }

        orgService.deleteById(deptId);

        return ReturnObjct.ok();
    }
}
